﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Tool;

namespace RooHui.Admin.Web.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/

        public ActionResult Index()
        {
            //ViewBag.roleList = RoleBiz.GetRoleList();
            return View();
        }
        [HttpPost]
        public ActionResult GetList()
        {
            var roleList = RoleBiz.GetRoleList();
            return Content(roleList.ToJson());
        }


        [HttpPost]
        public ActionResult RoleSave(string roleName, int roleID = 0)
        {

            if (RoleBiz.RoleExsit(roleName, roleID))
            {
                return Content("-1");
            }

            var role = RoleBiz.RoleSave(roleName, roleID, System.Web.HttpContext.Current.User.Identity.Name);
            if (role == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        [HttpPost]
        public ActionResult RoleDelete(int roleID)
        {
            var role = RoleBiz.DeleteRole(roleID);
            if (role == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        [HttpPost]
        public ActionResult GetModuleByRoleID(int roleID)
        {
            var list = RoleModuleBiz.GetRoleModuleByRoleID(roleID);
            if (list != null && list.Count > 0)
            {

                List<Dictionary<string, string>> datalist = new List<Dictionary<string, string>>();
                foreach (var item in list)
                {
                    var data = new Dictionary<string, string>();
                    data.Add("ID", item.module.ID.ToString());
                    data.Add("Name", item.module.Name);
                    data.Add("ParentID", item.module.ParentID.ToString());
                    data.Add("Icon", item.module.Icon);
                    if (item.RoleID == 0)
                    {
                        data.Add("ischecked", null);
                    }
                    else
                    {
                        data.Add("ischecked", "true");
                    }

                    datalist.Add(data);
                }
                return Content(datalist.ToJson());
            }
            return Content("");
        }
        [HttpPost]
        public ActionResult RoleAuthorize(int roleID, string moduleIDs)
        {
            if (RoleModuleBiz.RoleAuthorizeDelete(roleID))
            {
                RoleModuleBiz.RoleAuthorize(roleID, moduleIDs);
                return Content("1");
            }
            return Content("0");

        }

        [HttpPost]
        public ActionResult RoleAuthorizeDelete(int roleID)
        {
            if (RoleModuleBiz.RoleAuthorizeDelete(roleID))
            {
                return Content("1");
            }
            return Content("0");
        }

    }
}
